package com.meepo.framework.design.input;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.os.Build;
import android.util.AttributeSet;
import android.view.Gravity;

import androidx.annotation.DrawableRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatEditText;
import androidx.core.content.res.ResourcesCompat;

import com.meepo.framework.R;
import com.meepo.framework.design.helper.Utils;

public class InputView extends AppCompatEditText {

    private static final int DEF_SIZE = 36;

    private final Context mContext;

    private float mCornerRadius;
    private int mBackgroundColor;
    @DrawableRes
    private int mIconResId;
    @DrawableRes
    private int mIconRightResId;
    private int mSize;

    public InputView(@NonNull Context context) {
        this(context, null);
    }

    public InputView(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, android.R.attr.editTextStyle);
    }

    public InputView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext = context;

        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.InputView);

        int defHeight = Utils.dp2px(context, DEF_SIZE);

        mCornerRadius = ta.getDimension(R.styleable.InputView_sv_cornerRadius, defHeight / 2.0f);
        mBackgroundColor = ta.getColor(R.styleable.InputView_sv_backgroundColor, getResources().getColor(R.color.default_background));
        mIconResId = ta.getResourceId(R.styleable.InputView_sv_iconResId, 0);
        mIconRightResId = ta.getResourceId(R.styleable.InputView_sv_iconRightResId, 0);
        mSize = ta.getDimensionPixelSize(R.styleable.InputView_sv_size, defHeight);

        ta.recycle();


        notifyAttrsChanged();
    }

    private void notifyAttrsChanged() {


        this.setGravity(Gravity.CENTER_VERTICAL);
        this.setHeight(mSize);

        int iconSize = mSize / 5 * 2;
        int space = (mSize - iconSize) / 2;

        this.setPadding(space, 0, space, 0);

        GradientDrawable mDividerDrawable = new GradientDrawable();
        mDividerDrawable.setCornerRadius(mCornerRadius);
        mDividerDrawable.setColor(mBackgroundColor);
        this.setBackground(mDividerDrawable);

        if (mIconResId != 0) {
            Drawable icon = ResourcesCompat.getDrawable(mContext.getResources(), mIconResId, mContext.getTheme());
            if (icon != null) {
                icon.setBounds(0, 0, iconSize, iconSize);
                this.setCompoundDrawables(icon, null, null, null);
            }
        }

        if (mIconRightResId != 0) {
            Drawable icon = ResourcesCompat.getDrawable(mContext.getResources(), mIconRightResId, mContext.getTheme());
            if (icon != null) {
                icon.setBounds(0, 0, iconSize, iconSize);
                this.setCompoundDrawables(null, null, icon, null);
            }
        }

        this.setCompoundDrawablePadding(space);

        // 指示器
        int idWidth = Utils.dp2px(mContext, 1.5f);
        GradientDrawable indicator = new GradientDrawable();
        indicator.setColor(this.getTextColors());
        indicator.setSize(idWidth, -1);
        indicator.setCornerRadius(idWidth / 2f);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            this.setTextCursorDrawable(indicator);
        }
    }


    public void setIconRightResId(int mIconRightResId) {
        this.mIconRightResId = mIconRightResId;
        invalidate();
    }
}
